//StatusView Component Constructor
function StatusBarView(e) {

			
	//create the status view
	var self = Ti.UI.createView({

	});	

	//Create the connect button and set it to disabled and add it to the view
	//When the BlueTooth is paired, then enable this button
	var connectButton = Ti.UI.createButton({
		title: 'Connect',
		width: 100,
		height: 50,
		enabled: false,
		top: 5,
		left: 5
		
	})
	self.add(connectButton);
	
	//***************************************************** TODO *****************************************************
	//Listener for when BlueTooth has been connected
	//Enable the connect button
//	Ti.App.addEventListener('BlueToothConnect', function(evt) {		//global listener
//			connectButton.setEnabled(true);
//	  	});
//	});
	
	//Listener for click event to start the HUD, GPS, and Status activities after the BlueTooth connection
	//has been established
	//***************************************************** CHECK *****************************************************
	connectButton.addEventListener('click', function() {
		//start the hudActivity in the comms module
		Ti.App.fireEvent('startHUD', null);
		
		//start the gpsActivity in the comms module
		Ti.App.fireEvent('startGPS', null);
		
		//start the statusActivity in the comms module
		Ti.App.fireEvent('startStatus', null);
		
		connectButton.setEnabled(false);
	})
	
	//Create a label for the GPS status and add it to the view
	//Update the text property to display the GPS status
	var GPSstatus = Ti.UI.createLabel({
		text: 'GPS ',
		width: 100,
		height: 50,
		top: 5,
		left: e.width*0.2
		
	})
	self.add(GPSstatus);
	
	//Create a label for the battery voltage and add it to the view
	//Update the text property to display the battery voltage
	var batVolt = Ti.UI.createLabel({
		text: 'Voltage ',
		width: 150,
		height: 50,
		top: 5,
		left: e.width*0.45
		
	})
	self.add(batVolt);
	
	//Create a label for the battery remaining and add it to the view
	//Update the text property to display the battery remaining
	var batRemain = Ti.UI.createLabel({
		text: 'Remaining',
		width: 150,
		height: 50,
		top: 5,
		left: e.width*0.7
		
	})
	self.add(batRemain);
	
	//Create a button for the system status and add it to the view
	//Rather then messing with images, just change the background color to display the status
	//Should remain disabled
	var sysStatus = Ti.UI.createButton({
		width: 50,
		height: 50,
		top: 5,
		right: 5,
		backgroundColor: 'red',
		enabled: false
		
	})
	self.add(sysStatus);

	//Listeners for status updates from the copter. 
	//***************************************************** CHECK *****************************************************
	//Battery Status Data listener
	Ti.App.addEventListener('newStatusBatdata', function(evtBat) {		//global listener
		Titanium.API.log("debug","New Battery Status Event Received");
		
		batVolt.setText('Voltage ' + evtBat.volt);
		batRemain.setText('Remaining ' + evtBat.remain + '%');
	});
	
	//GPS Status Data listener
	Ti.App.addEventListener('newStatusGPSdata', function(evtGPS) {		//global listener
		Titanium.API.log("debug","New GPS Status Event Received");
		
		GPSstatus.setText('GPS Satellites Visible: ' + evtGPS.GPSStat);
	});

	//System Status Data listener
	Ti.App.addEventListener('newStatusSystemdata', function(evtStat) {		//global listener
		Titanium.API.log("debug","New System Status Event Received");
		
		updateStatusButton(evtStat.severity);
	});		
	
	Ti.App.addEventListener('heartbeat', function(evt) {;
		connectButton.setEnabled(true);
	});
	
	Ti.App.addEventListener('noheartbeat', function(evt) {
		statusLabel.text = 'CONNECTION LOST';
		statusLabel.color = '#ff0000';
	});

	//Update system status button on the status bar
	//	stat = status where 1 = good, 2 = error, 3 = critical 
	function updateStatusButton(stat)
	{
		switch(stat)
		{
			case 1:
			sysStatus.setBackgroundColor('green');
			break;
			case 2:
			sysStatus.setBackgroundColor('yellow');
			break;
			default:
			sysStatus.setBackgroundColor('red');
		}
	}
	
	return self;
}
module.exports = StatusBarView;